package top.dbpanda.cmsjpa.learn.juc;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class TestMyThreadPool {
    public static void main(String[] args) {
        /**
         * ThreadPoolExecutor
         * 1.核心最大数：1 如果只有一个任务，会直接交给线程池中的这一个线程来处理
         * 2.最大线程数：2 如果任务的数量>（核心数 1 + workQueue.size()）,
         * 且任务的数量<=最大线程数 2 +workQueue.size()之和时，就将新提交的任务交给非核心线程处理;
         * 3.最大空闲时间：10
         * 4.最大空闲时间的单位：秒
         * 5.任务队列：游街队列ArrayBlockingQueue,该队列可以存放3个任务
         * 6.拒绝策略：AbortPolicy(),当提交任务数 > （最大线程数2 + workQueue.size()）时，任务会交给AbortPolicy来处理
         */
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(1, 2, 10, TimeUnit.SECONDS,
//                new LinkedBlockingQueue<Runnable>() //无界队列
                new ArrayBlockingQueue<Runnable>(3)
                        ,
                //自定义拒绝策略
                new MyRejectPolicy()
//                new ThreadPoolExecutor.AbortPolicy()
        );
        MyThread t1 = new MyThread("t1");
        MyThread t2 = new MyThread("t2");
        MyThread t3 = new MyThread("t3");
        MyThread t4 = new MyThread("t4");
        MyThread t5 = new MyThread("t5");
        //ArrayBlockingQueue 大于最大线程树 + 队列的和会被及拒绝
        MyThread t6 = new MyThread("t6");
        poolExecutor.execute(t1);
        poolExecutor.execute(t2);
        poolExecutor.execute(t3);
        poolExecutor.execute(t4);
        poolExecutor.execute(t5);
        poolExecutor.execute(t6);
        poolExecutor.shutdown();
    }
}
